<?php
/*
Plugin Name: Google Maps Trace Route
Plugin URI: http://code.google.com/p/googlemapstraceroute/
Description: Use Google Maps to plot locations you have visited and draw the trace of your route.
Version: 1.2.3
Author: Jacek Karczmarczyk
Author URI: http://karczmarczyk.pl
*/

/*
Copyright 2010 Jacek Karczmarczyk

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * TODO
 * - resizable map in admin
 * - fix double insterting locations
 *
 * V2.0
 * - l10n
 * - tags instead of map name
 * - dialog for putting map in the post: map size, points visible, map name/tags
 * - managing on the map - drag/drop markers, click shows options dialog (name, mapname, publish etc)
 * - sorting points
 * 
 */

class GoogleMapsTraceRoute {
	const API_VERSION = "1.2.3";
	const DB_TABLE = "gmtr";

	private $_wpdb = null;
	
	public function __construct($wpdb) {
		$this->_wpdb = $wpdb;

		if ($this->installed()) {
			wp_register_script('google-maps-api-v3', 'http://maps.google.com/maps/api/js?sensor=true');
			wp_register_script('jquery-autocomplete', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/jquery.autocomplete.js');
			wp_register_script('gmtr-js', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/gmtr.js');
			wp_register_style('gmtr-css', WP_PLUGIN_URL.'/'.basename(dirname(__FILE__)).'/gmtr.css');

			add_action('admin_menu', array($this, 'admin_menu'));
			add_filter('plugin_action_links', array($this, 'action_links'), 10, 2);

			add_filter('init', array($this, 'init'));
			add_filter('admin_init', array($this, 'admin_init'));

			add_filter('the_content', array($this, 'content_filter'));
		}
	}

	public function installed() {
		$table_name = $this->_wpdb->prefix . self::DB_TABLE;
		return $this->_wpdb->get_var("show tables like '{$table_name}'") == $table_name;
	}

	public function install() {
		if ($this->installed()) return;
		
		$table_name = $this->_wpdb->prefix . self::DB_TABLE;
		$sql = "CREATE TABLE " . $table_name . " (
			gmtr_id BIGINT( 20 ) NOT NULL AUTO_INCREMENT PRIMARY KEY  ,
			gmtr_name VARCHAR( 100 ) NOT NULL,
			gmtr_mapname VARCHAR( 100 ) NOT NULL,
			gmtr_description VARCHAR( 255 ) NOT NULL,
			gmtr_lat DOUBLE NOT NULL,
			gmtr_lon DOUBLE NOT NULL,
			gmtr_publish VARCHAR( 3 ) NOT NULL,
			gmtr_route VARCHAR( 3 ) NOT NULL,
			gmtr_stamp TIMESTAMP NOT NULL
		);";

		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);

		add_option("gmtr_api_version", self::API_VERSION);
		add_option("gmtr_new_database", $sql);
		add_option("gmtr_map_width", '500');
		add_option("gmtr_map_height", '500');
		add_option("gmtr_zoomcounter", '10');
		add_option("gmtr_color", "0000FF");
	}

	public function uninstall() {
		if ($this->installed()) {
			$this->_wpdb->query("DROP TABLE IF EXISTS " . $this->_wpdb->prefix . self::DB_TABLE);
			delete_option("gmtr_api_version");
			delete_option("gmtr_new_database");
			delete_option("gmtr_map_width");
			delete_option("gmtr_map_height");
			delete_option("gmtr_zoomcounter");
			delete_option("gmtr_color");
		}
	}

	public function configuration() {
		$hidden_field_name = 'gmtr_submit_hidden';

		if( $_POST[ $hidden_field_name ] == 'Y' ) {
			$gmtr_zoomcounter = (int)$_POST['gmtr_zoomcounter'] >= 0 ? (int)$_POST['gmtr_zoomcounter'] : 7;
			$gmtr_map_width = (int)$_POST['gmtr_map_width'] > 0 ? (int)$_POST['gmtr_map_width'] : 500;
			$gmtr_map_height = (int)$_POST['gmtr_map_height'] > 0 ? (int)$_POST['gmtr_map_height'] : 500;
			$gmtr_color = trim(strtoupper($_POST['gmtr_color'])) === '' ? '0000FF' : trim(strtoupper($_POST['gmtr_color']));
			update_option('gmtr_zoomcounter', $gmtr_zoomcounter);
			update_option('gmtr_map_width', $gmtr_map_width);
			update_option('gmtr_map_height', $gmtr_map_height);
			update_option('gmtr_color', $gmtr_color);
			echo '<div class="updated"><p><strong>Google Maps Trace Route configuration options have been saved.</strong></p></div>';
		}

		$gmtr_zoomcounter = (int)get_option('gmtr_zoomcounter') >= 0 ? (int)get_option('gmtr_zoomcounter') : 7;
		$gmtr_map_width = (int)get_option('gmtr_map_width') > 0 ? (int)get_option('gmtr_map_width') : 500;
		$gmtr_map_height = (int)get_option('gmtr_map_height') > 0 ? (int)get_option('gmtr_map_height') : 500;
		$gmtr_color = trim(strtoupper(get_option('gmtr_color'))) === '' ? '0000FF' : trim(strtoupper(get_option('gmtr_color')));


		echo '<div class="wrap">';
		echo "<h2>Google Maps Trace Route » Configuration</h2>";
		echo "<form name=\"form1\" method=\"post\" action=\"".str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])."\">";
		echo "<input type=\"hidden\" name=\"{$hidden_field_name}\" value=\"Y\" />";
		echo "<div><em>The map zoom level will be calculated so that the last N markers will be visible. 0 will show all markers.</em></div>";
		echo "<div><label style=\"float: left; width: 12em; text-align: right; font-weight: bold\" for=\"gmtr_zoomcounter\">Zoom counter:</label> <input type=\"text\" name=\"gmtr_zoomcounter\" id=\"gmtr_zoomcounter\" value=\"".(int)$gmtr_zoomcounter."\" size=\"15\" /></div>";
		echo "<div><br/><em>Color of the route (hex values, i.e. 0000FF - blue, 888888 - gray, FF0000 - red, etc)</em></div>";
		echo "<div><label style=\"float: left; width: 12em; text-align: right; font-weight: bold\" for=\"gmtr_color\">Route color:</label> <input type=\"text\" name=\"gmtr_color\" id=\"gmtr_color\" value=\"".htmlspecialchars($gmtr_color)."\" size=\"15\" /></div>";
		echo "<div><br/><em>Set the default width and height of the map (in pixels).</em></div>";
		echo "<div><label style=\"float: left; width: 12em; text-align: right; font-weight: bold\" for=\"gmtr_map_width\">Map width:</label> <input type=\"text\" name=\"gmtr_map_width\" id=\"gmtr_map_width\" value=\"".(int)$gmtr_map_width."\" size=\"15\" /></div>";
		echo "<div><label style=\"float: left; width: 12em; text-align: right; font-weight: bold\" for=\"gmtr_map_height\">Map height:</label> <input type=\"text\" name=\"gmtr_map_height\" id=\"gmtr_map_height\" value=\"".(int)$gmtr_map_height."\" size=\"15\" /></div>";
		echo "<div class=\"submit\"><input style=\"margin-left: 13em\" type=\"submit\" name=\"Submit\" value=\"Update options »\" /></div>";
		echo "</form>";
		echo "</div>";
	}

	function add_location() {
		$hidden_field_name = 'gmtr_location_submit';

		if ($_POST[$hidden_field_name] == 'Y') {
			foreach($_POST as $varname=>$value){
				$$varname = $this->_wpdb->escape($value);
			}
			$lat = (float)$gmtr_lat;
			$lon = (float)$gmtr_lon;
			$gmtr_mapname = preg_replace('/[^a-z0-9]/', '', strtolower($gmtr_mapname));
			if ($gmtr_id) {
				$this->_wpdb->query("UPDATE {$this->_wpdb->prefix}" . self::DB_TABLE . " SET gmtr_name='$gmtr_name', gmtr_mapname='$gmtr_mapname',
				gmtr_lat='$lat', gmtr_lon='$lon', gmtr_description='$gmtr_description', gmtr_publish='$gmtr_publish',
				gmtr_route='$gmtr_route'
				WHERE gmtr_id=$gmtr_id");

			} else {
				$this->_wpdb->query("INSERT INTO {$this->_wpdb->prefix}" . self::DB_TABLE . " (
					gmtr_name, gmtr_mapname, gmtr_lat, gmtr_lon, gmtr_description, gmtr_publish, gmtr_route
				)
				values (
					'$gmtr_name', '$gmtr_mapname', '$lat', '$lon', '$gmtr_description', '$gmtr_publish', '$gmtr_route'
				)");
			}

			echo "<div class=\"updated\"><p><strong>".($gmtr_id ? 'Location updated!' : 'Location was added')."</strong></p></div>";
		}

		echo '<div class="wrap">';
		echo "<h2>Google Maps Trace Route » Add/edit location</h2>";
		echo "<form name=\"form1\" method=\"post\" action=\"".str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])."\">";
		echo "<input type=\"hidden\" name=\"{$hidden_field_name}\" value=\"Y\" />";
		if ($_GET['location']) {
			$location = $this->_wpdb->get_row("SELECT * FROM {$this->_wpdb->prefix}" . self::DB_TABLE . " WHERE gmtr_id=".$_GET['location']);
			echo "<input type=\"hidden\" name=\"gmtr_id\" value=\"".htmlspecialchars($_GET['location'])."\" />";
		} else {
			$location =  new stdClass();
			$location->gmtr_lat = $gmtr_lat;
			$location->gmtr_lon = $gmtr_lon;
			$location->gmtr_mapname = $gmtr_mapname;
			$location->gmtr_route = "yes";
		}
		?>
		<table class="optiontable">
		<tr>
			<td colspan="2" style="font-style:italic">Location name and description will be shown in the popup box after clicking the marker on the map.</td>
			<td rowspan="8" style="width: 400px; text-align: center; vertical-align: top; padding-right: 24px;">
				<div class="gmtr" id="gmtr_admin_map" style="width: 400px; height: 400px"><br/><br/><br/>Loading maps...</div>
				<input type="button" id="gmtr_center_admin_map" value="Locate marker"/>
				<script type="text/javascript">
				addLoadEvent(function() {
					var gmtr = new GoogleMapsTraceRoute(document.getElementById('gmtr_admin_map'), 'gmtr_lat', 'gmtr_lon');
					gmtr.initializeAdmin(new google.maps.LatLng(<?= (float)$location->gmtr_lat ?>, <?= (float)$location->gmtr_lon ?>));
					jQuery('#gmtr_center_admin_map').click(function () { gmtr.gotoMarker(); });
					jQuery('#gmtr_find_location').click(function () { gmtr.adminFindLocation(document.getElementById('gmtr_name').value); });
					jQuery('#gmtr_admin_map').resizable({ maxHeight: 800, maxWidth: 600, minHeight: 300, minWidth: 300 });
				});
				</script>
				<input style="display: none" title="Latitude" name="gmtr_lat" type="text" id="gmtr_lat" value="<?php echo (float)stripslashes($location->gmtr_lat); ?>" />
				<input style="display: none" title="Longtitude" name="gmtr_lon" type="text" id="gmtr_lon" value="<?php echo (float)stripslashes($location->gmtr_lon); ?>" />
			</td>
		</tr>
		<tr valign="top">
		<th style="text-align: right" scope="row"><label for="gmtr_name">Location name:</label></th>
		<td><input style="width: 240px" name="gmtr_name" type="text" id="gmtr_name" value="<?php echo htmlspecialchars(stripslashes($location->gmtr_name)); ?>" size="32" /><input type="button" id="gmtr_find_location" value="Search »" /></td>
		</tr>
		<tr valign="top">
		<th style="text-align: right" scope="row"><label for="gmtr_description">Description:</label></th>
		<td><textarea style="width: 280px" name="gmtr_description" id="gmtr_description" rows="5" cols="38"><?php echo htmlspecialchars(stripslashes($location->gmtr_description)); ?></textarea></td>
		</tr>
		<tr valign="top">
		<th style="text-align: right; white-space: pre" scope="row"><label for="gmtr_route">Marker mode:</label></th>
		<td>
			<select id="gmtr_route" name="gmtr_route">
				<option value="yes"<? if (stripslashes($location->gmtr_route) == 'yes') echo ' selected="selected"'; ?>>Visible</option>
				<option value="hid"<? if (stripslashes($location->gmtr_route) == 'hid') echo ' selected="selected"'; ?>>Hidden</option>
				<option value=""<? if (stripslashes($location->gmtr_route) == '') echo ' selected="selected"'; ?>>Detached</option>
			</select>
		</td>
		</tr>
		<tr valign="top">
		<th style="text-align: right" scope="row"><label for="gmtr_publish">Publish:</label></th>
		<td><input name="gmtr_publish" type="checkbox" id="gmtr_publish" value="yes" <? if(stripslashes($location->gmtr_publish) == 'yes' || !isset($location->gmtr_publish)) echo 'checked="checked"'; ?> /></td>
		</tr>
		<tr><td colspan="2" style="font-style:italic">
			You can use map name in post, i.e. if you create locations with map name "india" you can show locations only from india by putting [gmtr india] in your post.<br/>
			Only letters and digits are allowed.
		</td></tr>
		<tr valign="top">
		<th style="text-align: right" scope="row"><label for="gmtr_mapname">Map name:</label></th>
		<td>
			<input style="width: 280px" name="gmtr_mapname" type="text" id="gmtr_mapname" value="<?php echo htmlspecialchars(stripslashes($location->gmtr_mapname)); ?>" size="40" />
			<script type="text/javascript">jQuery(document).ready(function () { jQuery("input#gmtr_mapname").autocomplete(<?= json_encode($this->get_mapnames()) ?>, {autoFill: true}); });</script>
			</td>
			</tr>
			<tr valign="top">
				<th></th>
				<td style="text-align: left">
					<p class="submit"><input type="submit" name="Submit" value="Save location »" /> <a href="<?= admin_url('options-general.php?page=gmtr_manage_locations') ?>">Manage locations</a></p>
				</td>
			</tr>
			</table>
			</form>
		</div>
	<?php
	}

	function manage_locations() {
		echo '<div class="wrap">';
		echo "<h2>Google Maps Trace Route » Manage locations</h2>";

		if($_GET['action']){ // see if delete has been called
			if($_GET['sure']){ // delete the listing
				$this->_wpdb->query("DELETE FROM {$this->_wpdb->prefix}" . self::DB_TABLE . " WHERE gmtr_id=".$_GET['location']);
				echo "<div class=\"updated\"><p><strong>Location deleted!</strong></p></div>";
			}else{
				$location = $this->_wpdb->get_row("SELECT * FROM {$this->_wpdb->prefix}" . self::DB_TABLE . " WHERE gmtr_id=".$_GET['location']);
				echo '<div class="updated"><p>You are about to delete "<strong>' . htmlspecialchars(stripslashes($location->gmtr_name)) . '</strong>".  Are you sure you want to do that?<br /><a href="admin.php?page=gmtr_manage_locations&action=delete&sure=yep&location=' . $location->gmtr_id . '">yes</a> | <a href="admin.php?page=gmtr_manage_locations">no</a></p></div>';
			}
		}

		if (isset($_GET['gmtrf_mapname'])) update_option('gmtrf_mapname', $_GET['gmtrf_mapname']);
		if (isset($_GET['gmtrf_publish'])) update_option('gmtrf_publish', $_GET['gmtrf_publish']);
		if (isset($_GET['gmtrf_route'])) update_option('gmtrf_route', $_GET['gmtrf_route']);
		if (isset($_GET['gmtrf_search'])) update_option('gmtrf_search', $_GET['gmtrf_search']);

		?>
		<p><a href="admin.php?page=gmtr_add_location">Add a new location »</a></p>

		<form method="get" id="gmtr_filter">
			<div>
				<input type="hidden" name="page" value="<?= htmlspecialchars($_GET['page']) ?>" />
				<label for="gmtrf_mapname">Map</label>
				<select id="gmtrf_mapname" name="gmtrf_mapname">
					<option value=""<? if (get_option('gmtrf_mapname') == '') echo ' selected="selected"'; ?>>(all)</option>
					<?php
						$mapnames = $this->get_mapnames();
						foreach($mapnames as $mapname) {
							echo "<option value=\"".htmlspecialchars($mapname)."\"".(get_option('gmtrf_mapname') == $mapname ? ' selected="selected"' : '').">".htmlspecialchars($mapname)."</option>";
						}
					?>
				</select>
				<label for="gmtrf_publish">Published</label>
				<select id="gmtrf_publish" name="gmtrf_publish">
					<option value=""<? if (get_option('gmtrf_publish') == '') echo ' selected="selected"'; ?>>(all)</option>
					<option value="yes"<? if (get_option('gmtrf_publish') == 'yes') echo ' selected="selected"'; ?>>Yes</option>
					<option value="no"<? if (get_option('gmtrf_publish') == 'no') echo ' selected="selected"'; ?>>No</option>
				</select>
				<label for="gmtrf_route">Display mode</label>
				<select id="gmtrf_route" name="gmtrf_route">
					<option value=""<? if (get_option('gmtrf_route') == '') echo ' selected="selected"'; ?>>(all)</option>
					<option value="yes"<? if (get_option('gmtrf_route') == 'yes') echo ' selected="selected"'; ?>>Visible</option>
					<option value="hid"<? if (get_option('gmtrf_route') == 'hid') echo ' selected="selected"'; ?>>Hidden</option>
					<option value="det"<? if (get_option('gmtrf_route') == 'det') echo ' selected="selected"'; ?>>Detached</option>
				</select>
				<label for="gmtrf_search">Name/description</label>
				<input type="text" id="gmtrf_search" name="gmtrf_search" value="<?= htmlspecialchars(get_option('gmtrf_search')) ?>" />
				<input type="submit" value="Search »" />
			</div>
		</form>

		</form>
		<table class="widefat">
		<thead>
		<tr>
		<th scope="col">Map name</th>
		<th scope="col">Name</th>
		<th scope="col">Description</th>
		<th scope="col">Published</th>
		<th scope="col">Display mode</th>
		<th scope="col">Coordinates</th>
		<th scope="col" colspan="2" style="text-align: center;">Action</th>
		</tr>
		</thead>
		<tbody id="the-list">

		<?php
		$filter = array();
		if (trim(''.get_option('gmtrf_mapname')) !== '') $filter[] = "gmtr_mapname = '".$this->_wpdb->escape(get_option('gmtrf_mapname'))."'";
		if (get_option('gmtrf_publish') == 'yes') $filter[] = "gmtr_publish = 'yes'";
		if (get_option('gmtrf_publish') == 'no') $filter[] = "gmtr_publish <> 'yes'";
		if (get_option('gmtrf_route') == 'yes') $filter[] = "gmtr_route = 'yes'";
		if (get_option('gmtrf_route') == 'hid') $filter[] = "gmtr_route = 'hid'";
		if (get_option('gmtrf_route') == 'det') $filter[] = "gmtr_route <> 'yes' AND gmtr_route <> 'hid'";
		if (trim(''.get_option('gmtrf_search')) !== '') $filter[] = "CONCAT(gmtr_name, ' ', gmtr_description) LIKE '%".$this->_wpdb->escape(get_option('gmtrf_search'))."%'";
		if (sizeof($filter))
			$filter = "WHERE ".implode(" AND ", $filter);
		else
			$filter = '';

		$locations = $this->_wpdb->get_results("SELECT * FROM {$this->_wpdb->prefix}" . self::DB_TABLE . " {$filter} ORDER BY gmtr_id DESC");
		foreach ($locations as $location) {
			$route = stripslashes($location->gmtr_route);
			if ($route == 'yes') {
				$displayMode = 'Visible';
			}
			else if ($route == 'hid') {
				$displayMode = 'Hidden';
			}
			else {
				$displayMode = 'Detached';
			}
		?>
			<tr id='location-<?php echo $location->gmtr_id;?>' class='<?php echo $class;?>' valign="top">
			<td><?php echo htmlspecialchars(stripslashes($location->gmtr_mapname));?></td>
			<td><?php echo htmlspecialchars(stripslashes($location->gmtr_name));?></td>
			<td><?php echo strip_tags(stripslashes($location->gmtr_description));?></td>
			<td><?php echo htmlspecialchars(stripslashes($location->gmtr_publish));?></td>
			<td><?php echo htmlspecialchars($displayMode);?></td>
			<td><?php echo (float)stripslashes($location->gmtr_lat) . "<br />" . (float)stripslashes($location->gmtr_lon);?></td>
			<td><a href='admin.php?page=gmtr_add_location&location=<?php echo $location->gmtr_id;?>' class='edit'>Edit</a></td>
			<td><a href='admin.php?page=gmtr_manage_locations&action=delete&location=<?php echo $location->gmtr_id;?>' class='delete'>Delete</a></td>
			</tr>
		<?php
		}

		echo "</tbody></table></div>\n";
	}

	function get_mapnames() {
		$mapnames = array();
		$mapnamesdata = $this->_wpdb->get_results("SELECT DISTINCT gmtr_mapname FROM ".$this->_wpdb->prefix . self::DB_TABLE." WHERE gmtr_mapname <> '' ORDER BY gmtr_mapname");
		foreach($mapnamesdata as $mapname) $mapnames[stripslashes(trim($mapname->gmtr_mapname))] = stripslashes(trim($mapname->gmtr_mapname));
		return array_values($mapnames);
	}

	function content_filter_callback($match) {
		if (strstr($match[0], '?') === false) {
			$mapname = trim($match[1]);
			$showroute = true;
		}
		else {
			$query = str_replace(array('?', '&amp;'), array('', '&'), $match[1]);
			parse_str($query, $params);
			$points = (int)$params['points'];
			$width = (int)$params['width'];
			$height = (int)$params['height'];
			$mapname = $params['mapname'];
			$color = $params['color'];
			$showroute = isset($params['showroute']) ? (boolean)$params['showroute'] : true;
		}

		$mapid = uniqid(trim("gmtr_map_".$mapname, "_")."_");
		if (!$points) $points = (int)get_option('gmtr_zoomcounter');
		if (!$width) $width = (int)get_option('gmtr_map_width');
		if (!$height) $height = (int)get_option('gmtr_map_height');
		if (trim($color) === '') $color = (trim(get_option('gmtr_color')) === '' ? '0000FF' : trim(strtoupper(get_option('gmtr_color'))));

		$script = "(new GoogleMapsTraceRoute(document.getElementById('{$mapid}'), 'input_lat', 'input_lon')).initialize([\n";
		$mapmatch = $mapname === '' ? '' : " AND gmtr_mapname='".$this->_wpdb->escape($mapname)."'";
		$locations = $this->_wpdb->get_results($sql="SELECT * FROM {$this->_wpdb->prefix}" . self::DB_TABLE . " WHERE gmtr_publish='yes' AND (gmtr_lat <> 0 OR gmtr_lon <> 0) $mapmatch ORDER BY gmtr_id");
		foreach ($locations as $location) {
			$script .= '	'.json_encode(array((float)$location->gmtr_lat, (float)$location->gmtr_lon, stripslashes($location->gmtr_name), stripslashes($location->gmtr_description), $location->gmtr_route)).','."\n";
		}
		if (sizeof($locations)) {
			$script[strlen($script)-2] = " ";
		}
		$script .= "], {$points}, '#{$color}', ".(int)$showroute.");\n";

		return "\n<div class=\"gmtr\" style=\"width: {$width}px; height: {$height}px; \" id=\"{$mapid}\"></div>\n".
			   "<script type=\"text/javascript\">\n{$script}\n</script>\n";
	}

	function content_filter($content) {
		return preg_replace_callback('/\[gmtr(( +[a-z][0-9a-z]+)|(\?(width|height|points|mapname|color|showroute)=[a-z0-9]+(&(amp;)?(width|height|points|mapname|color|showroute)=[a-z0-9]+)*))?\]/i', array($this, 'content_filter_callback'), $content);
	}

	function init() {
		wp_enqueue_script('google-maps-api-v3');
		wp_enqueue_script('gmtr-js');
		wp_enqueue_style('gmtr-css');
	}

	function admin_init() {
		wp_enqueue_script('google-maps-api-v3');
		wp_enqueue_script('jquery-autocomplete');
		wp_enqueue_script('jquery-ui-resizable');
		wp_enqueue_script('gmtr-js');
		wp_enqueue_style('gmtr-css');
	}

	function action_links($links, $file) {
		if ($file == basename(dirname(__FILE__)).DIRECTORY_SEPARATOR.basename(__FILE__)) {
			array_unshift($links, '<a href="'.admin_url('options-general.php?page=gmtr_manage_locations').'">'.__('Manage locations').'</a>');
			array_unshift($links, '<a href="'.admin_url('options-general.php?page=gmtr_configuration').'">'.__('Settings').'</a>');
		}
		return $links;
	}

	function admin_menu() {
		add_options_page('Google Maps Trace Route configuration', 'GMTR', 10, 'gmtr_configuration', array($this, 'configuration'));
		add_utility_page('Manager locations', 'GMTR', 8, 'gmtr_manage_locations', array($this, 'manage_locations'));
		add_submenu_page('gmtr_manage_locations', 'Add/edit location', 'Add/edit location', 8, 'gmtr_add_location', array($this, 'add_location'));
	}
}

$gmtr = new GoogleMapsTraceRoute($GLOBALS['wpdb']);
register_activation_hook(__FILE__, array($gmtr, "install"));
register_uninstall_hook(__FILE__, array($gmtr, "uninstall"));

?>